package models

import (
	orm "go-papers/database"
	"go-papers/tools"
	"time"
	_ "time"
)

type SpecialContent struct {
	SpecialId     int    `json:"specialId" gorm:"type:int(11) unsigned;primary_key"` //
	SpecialCode   string `json:"specialCode" gorm:"type:varchar(64);"`               // 专题编号
	SpecialName   string `json:"specialName" gorm:"type:varchar(255);"`              // 专题名称
	SpecialImg    string `json:"specialImg" gorm:"type:varchar(255);"`               // 专题名称
	SpecialDesc   string `json:"specialDesc" gorm:"type:text;"`                      // 描述
	SpecialTxt    string `json:"specialTxt" gorm:"type:text;"`                       // 描述 - 富文本
	SpecialSort   int    `json:"specialSort" gorm:"type:int(11);"`                   // 权重 默认 100
	SpecialStatus string `json:"specialStatus" gorm:"type:tinyint(1);"`              // 状态 1 进行中 草稿  ，  5 发布 展示中 ， 10 已下线
	SpecialBanner string `json:"specialBanner" gorm:"type:varchar(255);"`            // banner图片地址
	SpecialStart  string `json:"specialStart" gorm:"type:datetime;"`                 // 有效日期 起始
	SpecialEnd    string `json:"specialEnd" gorm:"type:datetime;"`                   // 有效日期 结束
	SpecialTop    string `json:"specialTop" gorm:"type:varchar(255);"`               // 头图片
	SpecialGoods  string `json:"specialGoods" gorm:"type:text;"`                     // 商品信息 json
	SpecialBottom string `json:"specialBottom" gorm:"type:varchar(255);"`            // 底部 规则说明 图片
	CreateBy      string `json:"createBy" gorm:"type:varchar(255);"`                 // 创建人
	UpdateBy      string `json:"updateBy" gorm:"type:varchar(255);"`                 // 修改人
	UpdateName    string `gorm:"-" json:"updateName"`                                //
	DataScope     string `json:"dataScope" gorm:"-"`
	Params        string `json:"params"  gorm:"-"`
	BaseModel
}

func (SpecialContent) TableName() string {
	return "special_content"
}

// 创建SpecialContent
func (e *SpecialContent) Create() (SpecialContent, error) {
	var doc SpecialContent
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

// 获取SpecialContent
func (e *SpecialContent) Get() (SpecialContent, error) {
	var doc SpecialContent
	table := orm.Eloquent.Table(e.TableName())
	if e.SpecialId != 0 {
		table = table.Where("special_id = ?", e.SpecialId)
	}
	if e.SpecialName != "" {
		table = table.Where("special_name like ?", "%"+e.SpecialName+"%")
	}
	if e.SpecialStatus != "" {
		table = table.Where("special_status = ?", e.SpecialStatus)
	}
	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取SpecialContent带分页
func (e *SpecialContent) GetSearchPage(pageSize int, pageIndex int) ([]SpecialContent, int, error) {
	var doc []SpecialContent

	table := orm.Eloquent.Select("special_content.* ").Table(e.TableName())
	if e.SpecialId != 0 {
		table = table.Where("special_content.special_id = ?", e.SpecialId)
	}
	if e.SpecialCode != "" {
		table = table.Where("special_content.special_code like ?", "%"+e.SpecialCode+"%")
	}
	if e.SpecialName != "" {
		table = table.Where("special_content.special_name like ?", "%"+e.SpecialName+"%")
	}
	if e.SpecialStatus != "" {
		table = table.Where("special_content.special_status = ?", e.SpecialStatus)
	}

	dayNow := time.Now().Format("2006-01-02 15:04:05")
	table = table.Where("special_content.special_start <= ?", dayNow)
	table = table.Where("special_content.special_end >= ?", dayNow)

	var count int
	if err := table.Order(" `special_content`.updated_at DESC", true).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where(" `special_content`.`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 获取SpecialContent带分页
func (e *SpecialContent) GetPage(pageSize int, pageIndex int) ([]SpecialContent, int, error) {
	var doc []SpecialContent

	table := orm.Eloquent.Select("special_content.*,sys_user.nick_name as update_name ").Table(e.TableName())
	table = table.Joins("left join sys_user on sys_user.user_id = special_content.update_by")
	if e.SpecialId != 0 {
		table = table.Where("special_content.special_id = ?", e.SpecialId)
	}
	if e.SpecialCode != "" {
		table = table.Where("special_content.special_code like ?", "%"+e.SpecialCode+"%")
	}
	if e.SpecialName != "" {
		table = table.Where("special_content.special_name like ?", "%"+e.SpecialName+"%")
	}
	if e.SpecialStatus != "" {
		table = table.Where("special_content.special_status = ?", e.SpecialStatus)
	}
	if e.SpecialStart != "" {
		table = table.Where("special_content.special_start = ?", e.SpecialStart)
	}
	if e.SpecialEnd != "" {
		table = table.Where("special_content.special_end = ?", e.SpecialEnd)
	}
	if e.CreateBy != "" {
		table = table.Where("special_content.create_by = ?", e.CreateBy)
	}
	if e.UpdateBy != "" {
		table = table.Where("special_content.update_by = ?", e.UpdateBy)
	}

	// 数据权限控制(如果不需要数据权限请将此处去掉)
	dataPermission := new(DataPermission)
	dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
	table, err := dataPermission.GetDataScope(e.TableName(), table)
	if err != nil {
		return nil, 0, err
	}
	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where(" special_content.deleted_at IS NULL").Count(&count)
	return doc, count, nil
}

// 更新SpecialContent
func (e *SpecialContent) Update(id int) (update SpecialContent, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("special_id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除SpecialContent
func (e *SpecialContent) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("special_id = ?", id).Delete(&SpecialContent{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *SpecialContent) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("special_id in (?)", id).Delete(&SpecialContent{}).Error; err != nil {
		return
	}
	Result = true
	return
}
